<include file="public@header"/>
<script type="text/html" id="photos-item-tpl">
    <li id="saved-image{id}">
        <input id="photo-{id}" type="hidden" name="photo_urls[]" value="{filepath}">
        <input class="layui-input" id="photo-{id}-name" type="text" name="photo_names[]" value="{name}" title="图片名称" style="width:200px;">
   
        <img id="photo-{id}-preview" src="{url}" onclick="imagePreviewDialog(this.src);">
        <a href="javascript:uploadOneImage('{:lang('UPLOAD_IMAGE')}','#photo-{id}');">{:lang('REPLACE')}</a>
        <a href="javascript:(function(){$('#saved-image{id}').remove();})();">{:lang('REMOVE')}</a>
    </li>
</script>
<script type="text/html" id="files-item-tpl">
    <li id="saved-file{id}">
        <input id="file-{id}" type="hidden" name="file_urls[]" value="{filepath}">
        <input class="layui-input" id="file-{id}-name" type="text" name="file_names[]" value="{name}"
               style="width: 200px;" title="文件名称">
        <a id="file-{id}-preview" href="{preview_url}" target="_blank">下载</a>
        <a href="javascript:uploadOne('图片上传','#file-{id}','file');">替换</a>
        <a href="javascript:(function(){$('#saved-file{id}').remove();})();">移除</a>
    </li>
</script>
</head>
<body>
<div class="layui-fluid js-check-wrap">
    <div class="layui-card">
        <div class="layui-card-header">{:lang('GOODS_ADMINGOODS_ADD')}</div>
        <form action="{:url('AdminGoods/addPost')}" method="post" class=" js-ajax-form" name="theForm" id="theForm" data-replace="1">
            <div class="layui-card-body">
                <div class="layui-tab">
                    <ul class="layui-tab-title">
                        <li class="layui-this">
                            <span class="icon-wrapper"><i class="fa fa-align-center"></i></span>
                            <span class="text-wrapper">{:lang('GOODS_INFO')}</span>
                        </li>
                        <li>
                            <span class="icon-wrapper"><i class="fa fa-cog"></i></span>
                            <span class="text-wrapper">{:lang('GOODS_ATTR')}</span>
                        </li>
                        <li>
                            <span class="icon-wrapper"><i class="fa fa-cog"></i></span>
                            <span class="text-wrapper">{:lang('SEO_OPT')}</span>
                        </li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show layui-form">
                            <div class="layui-card-body">
                                <!-- 产品名称 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_NAME')}</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input form-replace" type="text" name="post[goods_name]" id="goods_name" data-field="goods_name"
                                                placeholder="{:lang('PLEASE_ENTER')}{:lang('GOODS_NAME')}"/>
                                    </div>
                                </div>
                                <!-- 产品名称 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">副标题</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input form-replace" type="text" name="post[subtitle]" id="subtitle" data-field="subtitle"
                                                placeholder="请输入产品副标题"/>
                                    </div>
                                </div>

                                <!-- 产品分类 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_CATEGORY')}</label>
                                    <div class="layui-input-block">
                                        <!-- 分类多选 -->
                                        <!-- <input class="layui-input" type="text"
                                               value="{$category['name']}"
                                               placeholder="{:lang('PLEASE_SELECTED_CATEGORY')}" onclick="doSelectCategory();" id="js-categories-name-input"
                                               readonly/>
                                        <input class="layui-input" type="hidden" value="{$category['id']}"
                                               name="post[categories]"
                                               id="js-categories-id-input"/> -->

                                        <!-- 分类单选 -->
                                        <select class="layui-input" name="post[categories]">
                                            <option value=''>{:lang('SELECT_PLEASE')}</option>
                                            {$category_tree|default=''}
                                        </select>
                                    </div>
                                </div>

                                <!-- 发布时间 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('ADD_TIME')}</label>
                                    <div class="layui-input-block">
                                        <div class="input-group">
                                            <input class="layui-input js-layui-datetime" type="text" name="post[published_time]"
                                                   value="{:date('Y-m-d H:i:s',time())}" readonly>
                                        </div>
                                    </div>
                                </div>

                                <!-- 产品简介 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_EXCERPT')}</label>
                                    <div class="layui-input-block">
                                        <!-- <textarea class="layui-input form-replace" name="post[post_excerpt]" rows="5" data-field="goods_excerpt" 
                                                  placeholder="{:lang('PLEASE_FILL_IN')}{:lang('GOODS_EXCERPT')}"></textarea> -->
                                        <script class="form-replace" type="text/plain" id="goods_excerpt" name="post[post_excerpt]">{$post.post_excerpt}</script>
                                    </div>
                                </div>

                                <!-- 详细内容 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('DETAIL_CONTENT')}</label>
                                    <div class="layui-input-block">
                                        <script class="form-replace" type="text/plain" id="detail_content" name="post[post_content]"></script>
                                    </div>
                                </div>
                               

                                <!-- 上传图片 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('UPLOAD_IMAGE')}</label>
                                
                                    <div class="layui-input-block">
                                        <div class="upload-image">
                                            <input type="hidden" name="post[more][thumbnail]" id="thumbnail"
                                                   value="">
                                            <a href="javascript:uploadOneImage('{:lang('UPLOAD_IMAGE')}','#thumbnail');">
                                                <img src="__TMPL__/public/assets/images/default-thumbnail.png"
                                                     id="thumbnail-preview"
                                                     width="150"/>
                                            </a>
                                            <input type="button" class="layui-btn layui-btn-danger layui-btn-md btn-cancel-thumbnail" value="{:lang('CANCEL_PICTURE')}">
                                        </div>
                                    </div>
                                </div>
                                

                                <!-- 图片推荐尺寸 -->
                                <notempty name="category['id']">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">{:lang('PICTURE_RECOMMENDED_SIZE')}</label>
                                        <div class="layui-col-lg5">
                                            <table class="thumb-size">
                                                <tr>
                                                    <td>{:lang('CATEGORY')}</td>
                                                    <td>{:lang('IMAGE_SIZE')}</td>
                                                </tr>
                                                <tr>
                                                    <td>{$category.name}</td>
                                                    <td>{$category.thumb_size}</td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </notempty>
                                
                                <!-- 相册 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('ALBUM')}</label>
                                    <div class="layui-input-block">
                                        <ul id="photos" class="pic-list list-unstyled form-inline"></ul>
                                        <a href="javascript:uploadMultiImage('{:lang("UPLOAD_IMAGE")}','#photos','photos-item-tpl');"
                                           class="layui-btn layui-btn-normal">{:lang('SELECT_PICTURE')}</a>
                                    </div>
                                </div>

                                <!-- 附件 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">上传文件</label>
                                    <div class="layui-input-block">
                                        <ul id="files" class="pic-list list-unstyled form-inline"></ul>
                                        <a href="javascript:uploadMultiFile('附件上传','#files','files-item-tpl');"
                                           class="layui-btn layui-btn-normal">选择文件</a>
                                    </div>
                                </div>
                               
                                <!-- 属性 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('ATTRIBUTE')}</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" name="post[is_top]" title="{:lang('IS_TOP')}" value="1">
                                        <input type="checkbox" name="post[recommended]" title="{:lang('RECOMMENDED')}" value="1">
                                    </div>
                                </div>

                                <!-- 模版 -->
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('GOODS_TEMPLATE')}</label>
                                
                                    <div class="layui-input-block">
                                        <select class="layui-input" name="post[more][template]" id="more-template-select">
                                            <foreach name="goods_theme_files" item="vo">
                                                <php>$value=preg_replace('/^goods\//','',$vo['file']);</php>
                                                <option value="{$value}">{$vo.name} {$vo.file}.html</option>
                                            </foreach>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
    
                        <div class="layui-tab-item goodsAttr">
                            <table>
                                <tr>
                                    <th class="control-label" style="border:0;"> {:lang('GOODS_ADMINGOODSTYPE_INDEX')} </th>
                                    <td class="" style="border:0;">
                                        <select class="form-control" name="post[goods_type]" onchange='getAttrList("{:url('AdminGoods/getAttr')}")'>
                                            <option value="">{:lang('SEL_GOODS_TYPE')}</option>
                                            {$goods_type_list}
                                        </select>
                                        <p class="layui-word-aux">{:lang('notice_goods_type')}</p>
                                    </td>
                                </tr>
                            </table>
                            <div id="tbody-goodsAttr">{$goods_attr_html}</div>
                        </div>

                        <div class="layui-tab-item layui-form">
                            <div class="layui-card-body">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('PAGE_TITLE')}</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input form-replace" type="text" name="post[page_title]" data-field="page_title"       placeholder="{:lang('PLEASE_ENTER')}{:lang('PAGE_TITLE')}">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('PAGE_KEYWORDS')}</label>
                                    <div class="layui-input-block">
                                        <input class="layui-input form-replace" type="text" name="post[page_keywords]" data-field="page_keywords"
                                               placeholder="{:lang('PLEASE_ENTER')}{:lang('PAGE_KEYWORDS')}">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">{:lang('PAGE_DESCRIPTION')}</label>
                                    <div class="layui-input-block">
                                        <textarea class="layui-textarea form-replace" id="post_excerpt" name="post[page_description]" rows="5"  data-field="page_description"
                                                  placeholder="{:lang('PLEASE_FILL_IN')}{:lang('PAGE_DESCRIPTION')}"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item padding-left-25">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn layui-btn-normal" id="add-key"
                                onclick="CheckSenSubmit();" >{:lang('ADD')}
                        <button type="submit" class="layui-btn layui-btn-normal js-ajax-submit" style="display:none;" id="js-ajax-submit">{:lang('ADD')}</button>
                        </button>
                        <!-- <button type="button" class="layui-btn layui-btn-normal content-preview" onclick="contentPreview(editorcontent,'goods_name');" id="add-key">{:lang('PREVIEW')}</button> -->
                        <a class="layui-btn layui-btn-primary" href="javascript:history.back(-1);">{:lang('BACK')}</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<include file="public@footer"/>

<script type="text/javascript" src="__STATIC__/js/transport.js"></script>
<script type="text/javascript" src="__STATIC__/js/utils.js"></script>
<script type="text/javascript">
    //编辑器路径定义
    var editorURL = GV.WEB_ROOT;
</script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.all.js"></script>


<script type="text/javascript">
    $(function () {
        if( $('#detail_content').length ){
            $('.content-preview').show();
            UE.getEditor('detail_content');
        }
    
        if( $('#goods_excerpt').length ){
            UE.getEditor('goods_excerpt');   
        }

        //移除缩略图
        $('.btn-cancel-thumbnail').click(function () {
            $('#thumbnail-preview').attr('src', '__TMPL__/public/assets/images/default-thumbnail.png');
            $('#thumbnail').val('');
        });
        //移除缩略图
        $('.btn-cancel-thumbnail2').click(function () {
            $('#thumbnail2-preview').attr('src', '__TMPL__/public/assets/images/default-thumbnail.png');
            $('#thumbnail2').val('');
        });

    });

    // 选择分类
    function doSelectCategory() {
        var selectedCategoriesId = $('#js-categories-id-input').val();
        openIframeLayer("{:url('AdminGoodsCategory/select')}?operate=add&ids=" + selectedCategoriesId, '请选择分类', {
            area: ['700px', '400px'],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                //do something

                var iframeWin          = window[layero.find('iframe')[0]['name']];
                var selectedCategories = iframeWin.confirm();
                if (selectedCategories.selectedCategoriesId.length == 0) {
                    layer.msg('请选择分类');
                    return;
                }
                $('#js-categories-id-input').val(selectedCategories.selectedCategoriesId.join(','));
                $('#js-categories-name-input').val(selectedCategories.selectedCategoriesName.join(' '));
                //console.log(layer.getFrameIndex(index));
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    }

    /**
     * 新增一个规格
     */
    function addSpec(obj)
    {
        var src   = obj.parentNode.parentNode;
        var idx   = rowindex(src);
        var tbl   = document.getElementById('attrTable');
        var row   = tbl.insertRow(idx + 1);
        var cell1 = row.insertCell(-1);
        var cell2 = row.insertCell(-1);
        var regx  = /<a([^>]+)<\/a>/i;

        cell1.className = 'th-label';
        cell1.innerHTML = src.childNodes[0].innerHTML.replace(/(.*)(addSpec)(.*)(\[)(\+)/i, "$1removeSpec$3$4-");
        cell2.innerHTML = src.childNodes[1].innerHTML.replace(/readOnly([^\s|>]*)/i, '');
    }

    /**
     * 删除规格值
     */
    function removeSpec(obj)
    {
        var row = rowindex(obj.parentNode.parentNode);
        var tbl = document.getElementById('attrTable');

        tbl.deleteRow(row);
    }
</script>
